home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Source Code ƒ / Misc. C ƒ / Recorder / Recorder.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-14  |  7.3 KB  |  304 lines  |  [TEXT/MPS ]

  1. /*    File: Recorder.c
  2.  
  3.     MPW Tool for recording serial line traffic
  4.     
  5. */
  6.  
  7.  
  8. #include <Types.h>
  9. #include <QuickDraw.h>
  10. #include <Files.h>
  11. #include <CursorCtl.h>
  12. #include <Strings.h>
  13.  
  14. #include <CRMIntf.h>
  15. #include <CTBUtils.h>
  16. #include <CMIntf.h>
  17. #include <FTIntf.h>
  18. #include <TMIntf.h>
  19.  
  20. #include <StdIO.h>
  21. #include <StdLib.h>
  22.  
  23. pascal void SetupGlob ( void );
  24. pascal void ReadCompletionGlue1  ( ConnHandle hConn );
  25. pascal void ReadCompletionGlue2  ( ConnHandle hConn );
  26. pascal void WriteCompletionGlue1 ( ConnHandle hConn );
  27. pascal void WriteCompletionGlue2 ( ConnHandle hConn );
  28.  
  29. #define    CommToolBoxTrap        0x8B
  30. #define    UnimplementedTrap    0x9F
  31. #define    Check(err,str)    { \
  32.                         OSErr errXYZZY; \
  33.                         if (( errXYZZY = ( err )) != noErr ) \
  34.                             fprintf ( stderr, "Error %d calling %s\n", errXYZZY, str ); \
  35.                         else \
  36.                             fprintf ( stderr, "%s : noErr\n", str); \
  37.                         }
  38. #define    OUTCONFIGSTR    "Baud 2400 dataBits 8 Parity None StopBits 1 Port \"Modem Port\"" \
  39.                         " Handshake None HoldConnection False RemindDisconnect False"
  40. #define    INCONFIGSTR        "Baud 2400 dataBits 8 Parity None StopBits 1 Port \"Printer Port\"" \
  41.                         " Handshake None HoldConnection False RemindDisconnect False"
  42.  
  43. #define        BUF_SIZE    1024
  44. #define        FILE_SIZE    5120
  45.  
  46. short        procID1;
  47. short        procID2;
  48. ConnHandle    stream1;
  49. ConnHandle    stream2;
  50. char        buffer1    [ BUF_SIZE ];
  51. char        buffer2    [ BUF_SIZE ];
  52. long        readSize1;
  53. long        writeSize1;
  54. long        readSize2;
  55. long        writeSize2;
  56. CMFlags        inFlags1;
  57. CMFlags        inFlags2;
  58. short        fRefNum;
  59. short        curBuffer;
  60. short        lastBuffer;
  61. char        *nextChar;
  62. char        fBuffer1 [ FILE_SIZE ];
  63. char        fBuffer2 [ FILE_SIZE ];
  64.  
  65. /*    Is the Comm Toolbox actually installed ?? */
  66. Boolean        IsCTBInstalled    ( ) {
  67.     return NGetTrapAddress ( UnimplementedTrap, OSTrap ) !=
  68.             NGetTrapAddress ( CommToolBoxTrap, OSTrap );
  69.     }
  70.  
  71. short    InitAll ( void ) {
  72.     OSErr    err;
  73.  
  74.     InitGraf ( &qd.thePort );
  75. /*
  76.     InitFonts ();
  77.     InitWindows ();
  78.     InitMenus ();
  79.     TEInit ();
  80.     InitDialogs ( NULL );
  81.     InitCursor ();
  82. */
  83.  
  84.     InitCursorCtl ( NULL );
  85.     SetupGlob ();
  86.     
  87.     if ( !IsCTBInstalled ) {
  88.         fprintf ( stderr, "Comm Toolbox not installed!\n" );
  89.         return 1;
  90.         }
  91.  
  92. /*    Load up the Communications Toolbox */
  93.     (void) InitCTBUtilities ();
  94.     (void) InitCRM ();
  95.  
  96.     err = InitTM ();
  97.     if ( err == tmNoTools ) {
  98.         fprintf ( stderr, "No terminal tools found\n" );
  99.         return 2;
  100.         }
  101.     
  102.     err = InitCM ();
  103.     if ( err == cmNoTools ) {
  104.         fprintf ( stderr, "No connection tools found\n" );
  105.         return 2;
  106.         }
  107.     
  108.     err = InitFT ();
  109.     if ( err == ftNoTools ) {
  110.         fprintf ( stderr, "No file transfer tools found\n" );
  111.         return 2;
  112.         }        
  113.  
  114.     return 0;
  115.     }
  116.  
  117.  
  118. void ExitProc ( void ) {
  119.     long    numBytes;
  120.     char    *theBuffer;
  121.     
  122.     
  123.     theBuffer = curBuffer == 1 ? fBuffer1 : fBuffer2;
  124.     numBytes = nextChar - theBuffer;
  125.     if ( numBytes > 0 ) {
  126.         Check ( FSWrite ( fRefNum, &numBytes, theBuffer ), "Write - AtExit" );
  127.         }
  128.  
  129.     Check ( FSClose ( fRefNum ), "FSClose" );
  130.     DebugStr ( "\pBefore KillIO" );
  131.     
  132.     Check ( CMIOKill (stream1, cmDataIn), "CMIOKill - input");
  133.     Check ( CMIOKill (stream2, cmDataIn), "CMIOKill - output");
  134.  
  135. /*    Close the connection and dispose of the connection record */
  136.     if ( stream1 != NULL ) {
  137.         Check ( CMClose ( stream1, false, NULL, 0, true ), "CMCLose - input" );
  138.         CMDispose ( stream1 );
  139.         }
  140.  
  141.     if ( stream2 != NULL ) {
  142.         Check ( CMClose ( stream2, false, NULL, 0, true ), "CMCLose - output" );
  143.         CMDispose ( stream2 );
  144.         }
  145.     }
  146.  
  147. ConnHandle    InitStream ( short *procID ) {
  148.     CMBufferSizes    bSize;
  149.  
  150. /*    Open a connection tool */
  151.     bSize [ cmDataIn ] = BUF_SIZE;        bSize [ cmDataOut ] = BUF_SIZE;
  152.     bSize [ cmCntlIn ] = 0;                bSize [ cmCntlOut ] = 0;
  153.     bSize [ cmAttnIn ] = 0;                bSize [ cmAttnOut ] = 0;
  154.     bSize [ cmRsrvIn ] = 0;                bSize [ cmRsrvOut ] = 0;
  155.     *procID = CMGetProcID ( "\pSerial" );
  156.     return CMNew ( *procID, cmQuiet + cmNoMenus, bSize, 0L, 0L );
  157.     }
  158.  
  159.  
  160. /*    Called at interrupt level */                
  161. pascal void XferBytes ( long sz, char *buffer, char dir ) {
  162.  
  163.     while ( sz-- > 0 ) {
  164.         *nextChar++ = dir;
  165.         *nextChar++ = *buffer++;
  166.         if ( curBuffer == 1 ) {
  167.             if ( nextChar - fBuffer1 == FILE_SIZE ) {
  168.                 nextChar = fBuffer2;
  169.                 curBuffer = 2;
  170.                 }
  171.             }
  172.         else {
  173.             if ( nextChar - fBuffer2 == FILE_SIZE ) {
  174.                 nextChar = fBuffer1;
  175.                 curBuffer = 1;
  176.                 }
  177.             }
  178.         }
  179.     }
  180.  
  181. /*    Interrupt routine */
  182. pascal void ReadCompletion1 ( ConnHandle hConn ) {
  183.     
  184.     if ((*hConn)->errCode == noErr ) {
  185.         writeSize1 = (*hConn)->asyncCount [ cmDataIn ];
  186.         XferBytes ( writeSize1, buffer1, '1' );
  187.         CMWrite ( stream2, buffer1, &writeSize1, 
  188.                     cmData, true, (ProcPtr) WriteCompletionGlue1, 0, inFlags1 );
  189.         }
  190.     }
  191.  
  192.  
  193. /*    Interrupt routine */
  194. pascal void WriteCompletion1 ( ConnHandle hConn ) {
  195.     
  196. /*    re-enable the read */
  197.     if ((*hConn)->errCode == noErr ) {
  198.         readSize1 = 1;
  199.         CMRead ( stream1, buffer1, &readSize1,
  200.                 cmData, true, (ProcPtr) ReadCompletionGlue1, 0, &inFlags1 );
  201.         }
  202.     }
  203.  
  204.  
  205. /*    Interrupt routine */
  206. pascal void ReadCompletion2 ( ConnHandle hConn ) {
  207.  
  208.     if ((*hConn)->errCode == noErr ) {
  209.         writeSize2 = (*hConn)->asyncCount [ cmDataIn ];
  210.         XferBytes ( writeSize2, buffer2, '2' );
  211.         CMWrite ( stream1, buffer2, &writeSize2, 
  212.                     cmData, true, (ProcPtr) WriteCompletionGlue2, 0, inFlags2 );
  213.         }
  214.     }
  215.  
  216.  
  217. /*    Interrupt routine */
  218. pascal void WriteCompletion2 ( ConnHandle hConn ) {
  219.  
  220. /*    re-enable the read */
  221.     if ((*hConn)->errCode == noErr ) {
  222.         readSize2 = 1;
  223.         CMRead ( stream2, buffer2, &readSize2,
  224.                 cmData, true, (ProcPtr) ReadCompletionGlue2, 0, &inFlags2 );
  225.         }
  226.     }
  227.  
  228.  
  229. int main ( int argc, char *argv[] ) {
  230.     short    err;
  231.     long    numBytes, cnt;
  232.     char    *theBuffer;
  233.     
  234.     if ( err = InitAll ( ) != 0 )
  235.         exit ( err );
  236.     
  237.     if ( argc != 2 ) {
  238.         fprintf ( stderr, "Usage: %s <fileName>\n", argv [ 0 ] );
  239.         exit ( 1 );
  240.         }
  241.     
  242.     c2pstr ( argv [ 1 ] );
  243.     curBuffer = lastBuffer = 1;
  244.     nextChar = fBuffer1;    
  245.     Check ( Create ( argv [ 1 ], 0, 'MPS ', 'TEXT' ), "Create" );
  246.     Check ( FSOpen ( argv [ 1 ], 0, &fRefNum ), "Open" );
  247.     Check ( SetFPos ( fRefNum, fsFromStart, 0L ), "SetFPos" );
  248.     atexit ( ExitProc );
  249.  
  250. /*    Open a connection tool */
  251.     stream1 = InitStream ( &procID1 );
  252.     if ( stream1 == NULL ) {
  253.         fprintf ( stderr, "Cannot create input handle\n" );
  254.         return 3;
  255.         }
  256.         
  257. /*    Open another connection tool */
  258.     stream2 = InitStream ( &procID1 );
  259.     if ( stream2 == NULL ) {
  260.         fprintf ( stderr, "Cannot create output handle\n" );
  261.         return 3;
  262.         }
  263.  
  264. /*    Configure the connection */
  265.  //    DebugStr ( "\pConfig" );
  266.     Check ( CMSetConfig ( stream1, INCONFIGSTR ), "CMSetConfig - In" );
  267.     Check ( CMOpen ( stream1, false, NULL, -1 ), "CMOpen - In" );
  268.     Check ( CMListen ( stream1, false, NULL, -1 ), "CMListen - In" );
  269.     
  270.     Check ( CMSetConfig ( stream2, OUTCONFIGSTR ), "CMSetConfig - Out" );
  271.     Check ( CMOpen ( stream2, false, NULL, -1 ), "CMOpen - Out" );
  272.     Check ( CMListen ( stream2, false, NULL, -1 ), "CMListen - Out" );
  273.  
  274. /*    Do an ansych read on the input stream */
  275.     readSize1 = 1;
  276.     Check ( CMRead ( stream1, buffer1, &readSize1,
  277.                                 cmData, true, (ProcPtr) ReadCompletionGlue1, 0, &inFlags1 ), "CMRead" );
  278.     readSize2 = 1;
  279.     Check ( CMRead ( stream2, buffer2, &readSize2,
  280.                                 cmData, true, (ProcPtr) ReadCompletionGlue2, 0, &inFlags2 ), "CMRead" );
  281.     cnt = 0;
  282.     while ( true ) {
  283.         
  284.     /*    write out any buffered data */
  285.         if ( lastBuffer != curBuffer ) {
  286.             fprintf ( stderr, "Writing File\n" );
  287.             numBytes = FILE_SIZE;
  288.             theBuffer = lastBuffer == 1 ? fBuffer1 : fBuffer2;
  289.             Check ( FSWrite ( fRefNum, &numBytes, theBuffer ), "FSWrite" );
  290.             lastBuffer = curBuffer;
  291.             }
  292.         
  293.         cnt++;
  294.         if ( cnt % 256 == 0 ) {
  295.             CMIdle ( stream1 );
  296.             CMIdle ( stream2 );
  297.             SpinCursor ( 1 );
  298.             }
  299.             
  300.         }
  301.  
  302.     return 0;
  303.     }
  304.